/*
 * Copyright (c) 2006, 2023, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

/**
 * <p>Project: jse2302 - T9
 * <p>Powered by webrx On 2023-06-30 17:49:24
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class T9 {
    Object a = new Object();
    Object b = new Object();

    void t1() {
        synchronized (a) {
            String t = Thread.currentThread().getName();
            System.out.println(t + "线程启动");
            try{
                Thread.sleep(10000);
            }catch(Exception e){
                e.printStackTrace();
            }
            synchronized (b) {
                System.out.println(a + "执行");
            }
            System.out.println(t + "线程结束");
        }
    }

    void t2() {
        synchronized (b) {
            String t = Thread.currentThread().getName();
            System.out.println(t + "线程启动");
            try{
                Thread.sleep(10000);
            }catch(Exception e){
                e.printStackTrace();
            }
            synchronized (a) {
                System.out.println(b + "执行");
            }
            System.out.println(t + "线程结束");
        }
    }


    public static void main(String[] args) {
        var t = new T9();
        var a = new Thread(t::t1, "A");
        var b = new Thread(t::t2, "B");
        a.start();
        b.start();
    }
}
